package sem.droid;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SemDroidActivity extends Activity {
	
	EditText queryText;
	EditText resultsText;
	ListView distanceText;
	EditText endpointText;
	TextView locationText;
	
	QueryEngine queryEngine;
	Double[] location = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        locationText = (TextView) findViewById(R.id.location_text);
        queryText = (EditText) findViewById(R.id.query_text);
        resultsText = (EditText) findViewById(R.id.results_text);
        endpointText = (EditText) findViewById(R.id.endpoint_text);
        queryEngine = new QueryEngine(endpointText.getText().toString());

        Button queryButton = (Button) findViewById(R.id.query_button);
        queryButton.setOnClickListener(new View.OnClickListener() {

        	public void onClick(View view) {
                String query = queryText.getText().toString();
                try {
                	List<String> results = queryEngine.execute(query, "x", false);
                	if( location != null )
                	{
                		TreeMap<Double,String> sorted = queryEngine.sortByDistance(location, results);
                		resultsText.setText(queryEngine.toString(sorted));
                	}
                	else {
                		resultsText.setText(queryEngine.toString(results));
                	}
                }
                catch(Exception e){
                	e.printStackTrace();
                }
            }
        });
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new LocationListener() {
           public void onLocationChanged(Location loc) {
        	   location = new Double[]{ loc.getLatitude(), loc.getLongitude() };
        	   locationText.setText("latitude="+ location[0] + 
                       " / longitude=" + location[1] );
           }
           public void onStatusChanged(String provider, int status, Bundle extras) {}
           public void onProviderEnabled(String provider) {}
           public void onProviderDisabled(String provider) {}
        };
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        // locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);  
    }
 }